﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Linq;


using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_Brand。
    /// </summary>
    public class BrandDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(Brand model)
        {
            using (Entities db = new Entities())
            {
                db.Brands.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Brand model)
        {
            using (Entities db = new Entities())
            {
                db.Brands.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Brands where Ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.Brands.Remove(x));

                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Brand GetInfo(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.Brands.Find(Id);
            }
        }
        #endregion

        #region  绑定DDL 控件
        /// <summary>
        /// 绑定DDL 控件
        /// </summary>
        public IList<Brand> Bind_DDL()
        {
            using (Entities db = new Entities())
            {
                return db.Brands.Where(x => x.IsShow == 1).OrderByDescending(x => x.OrderId).ToList();
            }
        }
        #endregion

        #region 取得前１８条数据
        /// <summary>
        /// 取得前１８条数据
        /// </summary>
        /// <returns></returns>
        public IList<Brand> Get18Brands()
        {
            using (Entities db = new Entities())
            {
                return db.Brands.Take(18).Where(x => x.IsShow == 1).OrderByDescending(x => x.OrderId).ToList();
            }
        }
        #endregion

        #region 从Ids中取得内容有多少个品牌，每个品牌有多少内容的数量
        /// <summary>
        /// 从Ids中取得内容有多少个品牌，每个品牌有多少内容的数量
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public IList<Brand> GetBrandInfoFromIds(IEnumerable<int> ids)
        {
            using (Entities db = new Entities())
            {
                var query= db.Brands.Where(x => x.ShopList.Any(y => ids.Contains(y.Id))).ToList();

                return query;
            }
        }

        #endregion

        #region 绑定分页数据
        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Brand> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.Brands
                            select x;
                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }
        #endregion
    }
}